package com.inossem.warehousestorage_inossem.adapter.inStorage.chuangyou;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou.CYChangeStorageManageActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou.CYInStorageShoppingListActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou.CYInStorageShoppingOverViewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageShoppingListBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventorySupplierListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/27 19:41
 * @email siyu.wang@inossem.com
 */
public class CYShoppingInStorageListAdapter extends BaseAdapter {
    private Context mContext;
    private List<CYInStorageShoppingListBean.ShoppingListBean> list = new ArrayList<>();
    private LayoutInflater inflater;
    private String code;
    private EditText codeEditText;
    private CYInStorageShoppingListActivity.CYInStorageShoppingListener listener;

    public CYShoppingInStorageListAdapter(Context context , String condition , List<CYInStorageShoppingListBean.ShoppingListBean> list , CYInStorageShoppingListActivity.CYInStorageShoppingListener listener) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        this.listener = listener;
        this.code = condition;
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.cy_view_change_storage_list_first_item, null);
            codeEditText = (EditText) convertView.findViewById(R.id.code);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            codeEditText.setHint(R.string.cy_activity_in_storage_shopping_list_production_of_put_in_storage_please_input_arrival_notice_number);
            if(!TextUtils.isEmpty(code)){
                codeEditText.setText(code);
            }
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    CYShoppingInStorageListAdapter.this.code = codeEditText.getText().toString();
//                    getData(mContext, CYProductionInStorageListAdapter.this.code);
                    listener.onNoDoubleClickListener(TextUtils.isEmpty(codeEditText.getText().toString())? "" : codeEditText.getText().toString());
                }
            });
        } else {
            CYInStorageShoppingListBean.ShoppingListBean materielItem = list.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.cy_view_shopping_in_storage_list_item, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.orderNumber = (TextView) view.findViewById(R.id.orderNumber);
        viewHolder.purchaseOrderNumber = (TextView) view.findViewById(R.id.purchaseOrderNumber);
        viewHolder.supplier = (TextView) view.findViewById(R.id.supplier);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, CYInStorageShoppingListBean.ShoppingListBean item) {
        viewHolder.orderNumber.setText(item.getStock_input_code());
        viewHolder.purchaseOrderNumber.setText(item.getPurchase_order_code());
        viewHolder.supplier.setText(item.getSupplier_name());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(mContext , CYInStorageShoppingOverViewActivity.class);
                intent.putExtra(InStorageExtra.STOCK_INPUT_ID, item.getStock_input_id().toString());
                intent.putExtra(InStorageExtra.ORDER_NUMBER, item.getStock_input_code());
                mContext.startActivity(intent);
//                intent.putExtra(ChangeStorageExtra.SUPPLIER_ID,  viewHolder.description.getText().toString());
//                mContext.setResult(CYChangeStorageManageActivity.SELECT_SUPPLIER_CODE, intent);
//                mContext.finish();

            }
        });
    }

    private class ViewHolder {
        private TextView orderNumber, purchaseOrderNumber , supplier ;
    }
}
